<?php
include('../db_connect.php');

$request_course=true;
if($_POST["courseDept"] == ""){
	$request_course=false;
    echo "Sorry, could not validate course department.";
    return;
}
$str_length=strlen($_POST['courseDept']);
if($str_length<2 || $str_length >6){
    echo "Sorry, could not validate course department.";
    return;
}

if($_POST["courseNum"] == "" || !is_numeric($_POST['courseNum'])){
	$request_course=false;
    echo "Sorry, could not validate course number.";
    return;
}
$str_length=strlen($_POST['courseNum']);
if($str_length<2 || $str_length >5){
    echo "Sorry, could not validate course number.";
    return;
}
if(!$_POST["user_first"] || !$_POST["user_last"] || !$_POST["ulink_id"]){
	$request_course=false;
    echo "Sorry, there was an unexpected error. Please contact REACH.";
    return;
}

$courseDept = $_POST['courseDept'];
$courseNum = $_POST['courseNum'];
$user_first = $_POST['user_first'];
$user_last = $_POST['user_last'];
$ulink = $_POST['ulink_id'];
$i_courseNum = (int) $courseNum;

//make sure this course does exist
$sql="select id from fb_courses where course_dept='$courseDept' and course_num=$i_courseNum";
$result = $db->Execute($sql);
$course_id = $result->fields['id'];
if(!$course_id)
{
    $request_course=false;
}

//we are validated, send the request to Reach 
if($request_course){ 
    $subject= "Study Buddies Course Request";
    $message= "This automated message has been sent due to a request from a student using the Study Buddies Facebook application. The following information details this request: \n\n Student ULink: $ulink \n Student Name: $user_first $user_last \n Course Name: $courseDept $courseNum \n Please verify this student is taking this course and add the student to the Study Buddies course group.";
    $headers = "From: REACH\r\n" . "X-Mailer: php";
    mail($reach_email, $subject,$message,$headers);
    echo "Thank you! A Reach administrator will verify your enrollment in $courseDept $courseNum and add you to the course.";

}
else{
    $subject= "Study Buddies Course Request";
    $message= "This automated message has been sent due to a request from a student using the Study Buddies Facebook application. The following information details this request: \n\n Student ULink: $ulink \n Student Name: $user_first $user_last \n Course Name: $courseDept $courseNum \n This course currently does not exist, so it must be added in the Reach Panel of the Study Buddies application before students can be added. \n Please verify this student is taking this course and add the student to the Study Buddies course group after it is created.";
    $headers = "From: REACH\r\n" . "X-Mailer: php";
    mail($reach_email, $subject,$message,$headers);
    echo "Thank you! A Reach administrator will verify your enrollment in $courseDept $courseNum and add you to the course.";
}
?>
